package com.lysj.merchant.desk.controller;

import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseController;
import com.lysj.admin.utils.validation.annotation.Valid;
import com.lysj.admin.utils.web.Resp;
import com.lysj.merchant.desk.domain.Desk;
import com.lysj.merchant.desk.dto.DeskAddDto;
import com.lysj.merchant.desk.dto.DeskQueryDto;
import com.lysj.merchant.desk.feign.OrderFeign;
import com.lysj.merchant.desk.service.DeskService;
import javafx.scene.input.InputMethodTextRun;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.util.*;

/**
 * @author Created by drj on 2019-03-07 14:35:25
 * @description 餐桌控制层
 */
@Slf4j
@RestController
@RequestMapping("/merchant/desk")
public class DeskController extends BaseController<Desk> {


    @Resource
    private DeskService deskService;

    @Override
    public DeskService getService() {
        return deskService;
    }

    @Resource
    private OrderFeign orderFeign;


    @GetMapping("/list_rewrite")
    public Resp listRewrite(DeskQueryDto desk) {
        return new Resp().success(deskService.findDeskList(desk));
    }

    @PostMapping("/save_rewrite")
    public Resp saveRewrite(@Valid DeskAddDto deskDto) throws IOException {
        return new Resp().success(deskService.saveRewrite(deskDto));
    }

    /*
     * @author drj
     * @date 2019-03-12 16:36
     * @Description :锁定餐桌换桌
     */
    @PostMapping("/lock_desk_change")
    public Resp lockDeskChange(String sourceDeskId, String targetDeskId) {
        Desk sourceDesk = deskService.findOne(sourceDeskId);
        Desk targetDeskDesk = deskService.findOne(targetDeskId);
        //改变两个餐桌信息
        targetDeskDesk.setCusNumber(sourceDesk.getCusNumber());
        targetDeskDesk.setStatus(Desk.Status.LOCK.getCode());
        targetDeskDesk.setCustomerId(sourceDesk.getCustomerId());
        sourceDesk.setCusNumber(0);
        sourceDesk.setStatus(Desk.Status.FREE.getCode());
        sourceDesk.setCustomerId("");
        deskService.save(targetDeskDesk);
        deskService.save(sourceDesk);
        return new Resp().success("换桌成功");
    }

    /*
     * @author drj
     * @date 2019-04-12 11:41
     * @Description :就餐餐桌换桌
     */
    @PostMapping("/repast_desk_change")
    public Resp repastDeskChange(String sourceDeskId, String targetDeskId) {
        Desk sourceDesk = deskService.findOne(sourceDeskId);
        Desk targetDeskDesk = deskService.findOne(targetDeskId);
        //改变两个餐桌信息
        targetDeskDesk.setCusNumber(sourceDesk.getCusNumber());
        targetDeskDesk.setStatus(Desk.Status.REPAST.getCode());
        targetDeskDesk.setCustomerId(sourceDesk.getCustomerId());
        sourceDesk.setCusNumber(0);
        sourceDesk.setStatus(Desk.Status.FREE.getCode());
        sourceDesk.setCustomerId("");
        //改变就餐订单所绑定的餐桌id
        boolean flag = orderFeign.changeDesk(sourceDeskId, targetDeskId);
        if (!flag) {
            return new Resp().success("换桌失败");
        }
        deskService.save(targetDeskDesk);
        deskService.save(sourceDesk);
        return new Resp().success("换桌成功");
    }


    //显示桌台数据
    @GetMapping("/show_desk_info")
    public Resp showDeskInfo(String storeId) {
        Map<String, Integer> map = new HashMap<>();
        //空闲餐桌
        Integer freeDesk = deskService.getRepository().countByStoreIdAndStatusAndDelFlag(storeId, Desk.Status.FREE.getCode(), CommonConstant.NORMAL_FLAG);
        //就餐餐桌
        Integer repastDesk = deskService.getRepository().countByStoreIdAndStatusAndDelFlag(storeId, Desk.Status.REPAST.getCode(), CommonConstant.NORMAL_FLAG);
        //锁定餐桌
        Integer lockDesk = deskService.getRepository().countByStoreIdAndStatusAndDelFlag(storeId, Desk.Status.LOCK.getCode(), CommonConstant.NORMAL_FLAG);
        map.put("freeDesk", freeDesk);
        map.put("repastDesk", repastDesk);
        map.put("lockDesk", lockDesk);
        return new Resp().success(map);
    }

    /*
     * @author drj
     * @date 2019-03-12 17:23
     * @Description :获取门店空闲的餐桌
     */
    @GetMapping("/show_free_desks")
    public Resp showFreeDesks(String storeId, String deskRoomId, String deskTypeId) {
        return new Resp().success(deskService.showFreeDesks(storeId, deskRoomId, deskTypeId));
    }

    @Override
    public Resp logicalDel(String[] ids) {
        for (String id : ids) {
            Desk desk = deskService.findOne(id);
            String filePath = "C:/desk/" + desk.getStoreId() + "/waterMark/" + desk.getName() + ".png";
            File scFileDir = new File(filePath);
            scFileDir.delete();
        }
        return super.logicalDel(ids);
    }

    /*
     * @author drj
     * @date 2019-07-11 19:20
     * @Description :释放餐桌
     */
    @PostMapping("/release_desks")
    public Resp releaseDesks(String[] ids) {
        List<Desk> desklist = deskService.getRepository().findAll(Arrays.asList(ids));
        for (Desk desk : desklist) {
            //初始化餐桌信息
            desk.setCusNumber(0); //用餐人数设置为0
            desk.setCustomerId(""); //用户id设置为空
            desk.setStatus(Desk.Status.FREE.getCode()); //餐桌状态设置为空闲状态
        }
        deskService.save(desklist);
        return new Resp().success("操作成功");
    }
}
